package com.allen.qsbk.ui

import android.content.Intent
import android.view.KeyEvent
import androidx.fragment.app.Fragment
import com.allen.app.core.RouterHub
import com.allen.app.data.bean.*
import com.allen.app.data.event.EndHomeAnimEvent
import com.allen.app.data.event.UpdateMainEvent
import com.allen.app.helper.RouteHelper
import com.allen.base.base.activity.BaseActivity
import com.allen.base.base.adapter.BaseFragmentPageAdapter
import com.allen.base.widget.btm_nav_view.BtmNavItemView
import com.allen.base.widget.btm_nav_view.BtmNavViewListener
import com.allen.base.widget.btm_nav_view.LottieNavItemView
import com.allen.qsbk.R
import com.hwangjr.rxbus.RxBus
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.thread.EventThread
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
import kotlin.collections.ArrayList


/**
 * 主界面
 */
class MainActivity : BaseActivity(), BtmNavViewListener {

    override fun getLayoutResID() = R.layout.activity_main

    private val mFragments = ArrayList<Fragment>()

    private val items = TreeMap<Int, BtmNavItemView>()

    override fun isRegisterRxBus() = true

    override fun initViews() {
        super.initViews()
        loadFragments()
        val pageAdapter = BaseFragmentPageAdapter(supportFragmentManager, mFragments)
        mViewPager.adapter = pageAdapter
        bottomBar.create(items, 0)
        bottomBar.setBtmNavViewListener(this)

    }

    override fun onBtmNavViewClick(id: Int, oldId: Int) {
        mViewPager?.currentItem = id
    }

    override fun onBtmNavViewSameClick(id: Int) {
        RxBus.get().post(UpdateMainEvent(id))
    }

    private fun loadFragments() {
        loadHome()
        loadDynamic()
        loadLive()
        loadScrip()
        loadMe()
    }

    @Subscribe(thread = EventThread.MAIN_THREAD)
    fun onMessageEvent(messageEvent: EndHomeAnimEvent) {
        bottomBar?.endSameClickAnimation(messageEvent.index)
    }

    private fun loadHome() {
        val home = RouteHelper.obtainARouterFragment(RouterHub.ROUTER_INFO_FRAGMENT_INDEX)
        if (home != null) {
            mFragments.add(home)
            items[FRAGMENT_ID_INFO] = LottieNavItemView(
                this, FRAGMENT_ID_INFO, getString(R.string.app_menu_info)
                , animRes = R.drawable.anim_menu_loading1
                , unSelectedIconId = R.mipmap.ic_qiushi_normal
                , selectedIconId = R.mipmap.ic_qiushi_select
                , refreshAnimationIconId = R.mipmap.ic_qiushi_refresh
                , enableSameClickAnimation = true
            )
        }
    }

    private fun loadDynamic() {
        val dynamic = RouteHelper.obtainARouterFragment(RouterHub.ROUTER_DYNAMIC_FRAGMENT)
        if (dynamic != null) {
            mFragments.add(dynamic)
            items[FRAGMENT_ID_DYNAMIC] = LottieNavItemView(
                this, FRAGMENT_ID_DYNAMIC, getString(R.string.app_menu_dynamic)
                , animRes = R.drawable.anim_menu_loading2
                , unSelectedIconId = R.mipmap.ic_qiuyoucircle_normal
                , selectedIconId = R.mipmap.ic_qiuyoucircle_select
            )
        }
    }


    private fun loadLive() {
        val live = RouteHelper.obtainARouterFragment(RouterHub.ROUTER_LIVE_INDEX_FRAGMENT)
        if (live != null) {
            mFragments.add(live)
            items[FRAGMENT_ID_LIVE] = LottieNavItemView(
                this, FRAGMENT_ID_LIVE, getString(R.string.app_menu_live)
                , animRes = R.drawable.anim_menu_loading3
                , unSelectedIconId = R.mipmap.ic_live_normal
                , selectedIconId = R.mipmap.ic_live_select
            )
        }
    }

    private fun loadScrip() {
        val scrip = RouteHelper.obtainARouterFragment(RouterHub.ROUTER_SCRIP_FRAGMENT)
        if (scrip != null) {
            mFragments.add(scrip)
            items[FRAGMENT_ID_SCRIP] = LottieNavItemView(
                this, FRAGMENT_ID_SCRIP, getString(R.string.app_menu_scrip)
                , animRes = R.drawable.anim_menu_loading4
                , unSelectedIconId = R.mipmap.ic_message_normal
                , selectedIconId = R.mipmap.ic_message_select
            )
        }
    }

    private fun loadMe() {
        val me = RouteHelper.obtainARouterFragment(RouterHub.ROUTER_ME_FRAGMENT)
        if (me != null) {
            mFragments.add(me)
            items[FRAGMENT_ID_ME] = LottieNavItemView(
                this, FRAGMENT_ID_ME, getString(R.string.app_menu_me)
                , animRes = R.drawable.anim_menu_loading5
                , unSelectedIconId = R.mipmap.ic_mine_normal
                , selectedIconId = R.mipmap.ic_mine_select
            )
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            val home = Intent(Intent.ACTION_MAIN)
            home.addCategory(Intent.CATEGORY_HOME)
            startActivity(home)
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}